using System;
using System.IO;
using Kassandra.Core.WordWrapper;

namespace Kassandra.Core
{
    public class ProcessFile
    {
        private readonly IWord97Wrapper _wordDocument;
        private readonly ITranslate _translator;

        public delegate void DisplayScreenMessageDelegate(string message);
        public event DisplayScreenMessageDelegate DisplayScreenMessage;


        public ProcessFile():this(new Word97Wrapper(), new Translate())
        {
        }

        public ProcessFile(IWord97Wrapper wordDocument, ITranslate translator)
        {
            _wordDocument = wordDocument;
            _translator = translator;
        }

        public void MultipleWordInstances(string message)
        {
            if (DisplayScreenMessage != null)
            {
                DisplayScreenMessage(message);
            }
        }

        public void With(string sourceFile, string destinationFile)
        {
            string tempFileName = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());

            try
            {
                using (_wordDocument)
                {
                    _wordDocument.Load(sourceFile);
                    _wordDocument.SaveAsXmlDocument(tempFileName);
                }

                System.Threading.Thread.Sleep(1000);

                _translator.For(tempFileName, destinationFile);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (File.Exists(tempFileName))
                {
                    File.Delete(tempFileName);
                }
            }
        }
    }
}